package net.ck.hzq;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import Utils.CheckPhoneNumberUtils;
import base.BaseAct;
import butterknife.BindView;
import dao.JuheBean;
import event.NetErrorEvent;
import model.ListviewLpgisticsinfomationAdp;
import util.StringUtils;

public class LogisticsInformationAct extends BaseAct {

    List<JuheBean.ResultBean.ListBean> data;
    ListviewLpgisticsinfomationAdp adp;
    @BindView(R.id.logistics_list_view)
    ListView listView;
    @BindView(R.id.logistics_no_date)
    TextView noDate;
    @BindView(R.id.logistics_number)
    TextView logisticsNumber;
    @BindView(R.id.logistics_shop)
    TextView logisticsShop;
    String noDataInfo = "";

    @Override
    protected void setRoot() {
        setContentView(R.layout.activity_logistics_information);
    }

    @Override
    protected void setup() {
        setToolBar(getResources().getString(R.string.logisticsinformation), "");
    }

    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        listView.setDivider(null);
        loading(true);
        new Thread() {
            @Override
            public void run() {
                super.run();
                String name = getIntent().getStringExtra("name");
                String id = getIntent().getStringExtra("id");
                String phone = getIntent().getStringExtra("phone");
                logisticsNumber.setText("快递单号：" + id);
                logisticsShop.setText("承运公司：" + name);
                try {
                    String url = "http://v.juhe.cn/exp/index?key=e81a7bbfc8498091c6becc6a43e30829&com=" + name + "&no=" + id + "&receiverPhone=" + phone.substring(phone.length() - 4, phone.length());
                    String s = CheckPhoneNumberUtils.getHtml(url);
                    if (s != null) {
                        JSONObject object = new JSONObject(s);//获取json数据
                        int resultcode = object.getInt("resultcode");
                        if (resultcode == 200) {
                            JSONObject object1 = object.getJSONObject("result");//获取json数据
                            JSONArray jsonArray = object1.getJSONArray("list");//获取数据集名称为data的数据
                            if (jsonArray.length() > 0) {
                                data = new ArrayList<>();
                                for (int i = jsonArray.length() - 1; i >= 0; i--) {
                                    JuheBean.ResultBean.ListBean dataBean = new JuheBean.ResultBean.ListBean();
                                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                                    dataBean.setDatetime((String) jsonObject.get("datetime"));
                                    dataBean.setRemark((String) jsonObject.get("remark"));
//                                dataBean.setZone((String) jsonObject.get("zone"));
                                    data.add(dataBean);
                                }
                                handler.sendEmptyMessage(0x123);
                            } else {
                                handler.sendEmptyMessage(0x122);
                            }
                        } else {
                            String info = object.getString("reason");//获取json数据
                            if (info != null && !info.equals("")) {
                                noDataInfo = info;
                                handler.sendEmptyMessage(0x122);
                            }
                        }
                    } else {
                        handler.sendEmptyMessage(0x122);
                    }
                } catch (Exception e) {
                    handler.sendEmptyMessage(0x122);
                }
                loading(false);
            }
        }.start();
    }


    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    private Handler handler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            switch (msg.what) {
                case 0x123:
                    adp = new ListviewLpgisticsinfomationAdp(data, LogisticsInformationAct.this);
                    listView.setAdapter(adp);
                    adp.notifyDataSetChanged();
                    noDate.setVisibility(View.GONE);
                    listView.setVisibility(View.VISIBLE);
                    break;
                case 0x122:
                    noDate.setVisibility(View.VISIBLE);
                    listView.setVisibility(View.GONE);
                    if (!StringUtils.isEmpty(noDataInfo)) {
                        noDate.setText(noDataInfo);
                    }
                    break;
            }
        }
    };

}
